package com.gzhryc.shared_device.oem.h5.mng.controllers.ops;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.config.PropertyTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.config.dao.db.AppResourcePreload;
import com.gzhryc.shared_device.oem.code.config.dao.enums.AppResourcePreloadEnum;
import com.gzhryc.shared_device.oem.code.config.services.AppResourcePreloadService;
import com.gzhryc.shared_device.oem.code.config.services.dto.AppResourcePreloadSearch;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class AppResourcePreloadController {

    @Route(value = { "/ops/app_preload/list.html" })
    public String list(HttpServletRequest request) {
        request.setAttribute("typeList", AppResourcePreloadEnum.Type.values());
        return "/ops/app_preload/list.html";
    }

    @Route(value = "/ops/app_preload/list.data")
    public ListResult<AppResourcePreload> listData(HttpServletRequest request) {
        ListResult<AppResourcePreload> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        AppResourcePreloadSearch search = WebUtils.getParameter(request, AppResourcePreloadSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(AppResourcePreloadService.self().countBySearch(search));
        List<AppResourcePreload> appPreloadList = AppResourcePreloadService.self().findBySearch(search,pageNo,pageLimit);
        if(appPreloadList != null && appPreloadList.size() > 0){
            String resDomain = PropertyTools.get().getString("resDomain");
            for(AppResourcePreload appPreload : appPreloadList){
                if(StringTools.isNotBlank(appPreload.getPath())) {
                    appPreload.setPath(WebUtils.setHttpDomain(resDomain,appPreload.getPath()));
                }
            }
            result.setCode(DtoConstants.SUCCESS,null);
            result.getBody().setData(appPreloadList);
        }
        return result;
    }

    @Route(value = { "/ops/app_preload/add.html" })
    public String add(HttpServletRequest request) {
        request.setAttribute("typeList", AppResourcePreloadEnum.Type.values());
        return "/ops/app_preload/edit.html";
    }

    @Route(value = {"/ops/app_preload/add.do"})
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "新增失败，请稍后再试");
        AppResourcePreload appPreload = WebUtils.getParameter(request, AppResourcePreload.class);
        try {
            if(AppResourcePreloadService.self().add(appPreload)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/ops/app_preload/update.html" })
    public String update(HttpServletRequest request) {
        Long id = WebUtils.getLong(request,"id");
        AppResourcePreload appPreload = AppResourcePreloadService.self().getById(id);
        WebUtils.setAttribute(request,appPreload);
        request.setAttribute("typeList", AppResourcePreloadEnum.Type.values());
        return "/ops/app_preload/edit.html";
    }

    @Route(value = {"/ops/app_preload/update.do"})
    public AjaxResult doUpdate(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "修改失败，请稍后再试");
        AppResourcePreload appPreload = WebUtils.getParameter(request, AppResourcePreload.class);
        try {
            if(AppResourcePreloadService.self().updateNotNull(appPreload)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = {"/ops/app_preload/del.do"})
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除失败，请稍后再试");
        Long id = WebUtils.getLong(request,"id");
        if(AppResourcePreloadService.self().deleteById(id)){
            result.setCode(DtoConstants.SUCCESS,null);
        }
        return result;
    }
}
